package de.dopichaj.labrador.index.own;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import de.dopichaj.labrador.index.RAMIndex;
import de.dopichaj.labrador.index.WritableIndex;
import de.dopichaj.labrador.index.backend.Dictionary;
import de.dopichaj.labrador.index.backend.InvertedList;
import de.dopichaj.labrador.index.backend.InvertedListSearcher;
import de.dopichaj.labrador.index.backend.RAMInvertedList;
import de.dopichaj.labrador.index.backend.RAMInvertedListSearcher;
import de.dopichaj.labrador.index.backend.ReadOnlyDictionary;
import de.dopichaj.labrador.search.NEXISearcher;
import de.dopichaj.labrador.util.Configuration;
import de.dopichaj.labrador.util.io.ArrayByteReader;
import de.dopichaj.labrador.util.io.ByteReader;


public final class OwnRAMIndex extends RAMIndex implements OwnIndex {

    private ByteArrayOutputStream dictionaryOutput;
    private final RAMInvertedList invertedList = new RAMInvertedList();
    private final Dictionary dictionary;

    public OwnRAMIndex(String[] stopWords, Configuration configuration) {
        super(stopWords, configuration);
        this.dictionary = new Dictionary();
    }

    public InvertedList getInvertedList() {
        return invertedList;
    }

    public DataOutputStream getDictionaryOutputStream() throws IOException {
        dictionaryOutput = new ByteArrayOutputStream();
        return new DataOutputStream(dictionaryOutput);
    }

    public ByteReader getDictionaryInputStream() throws IOException {
        assert dictionaryOutput != null;
        
        return new ArrayByteReader(dictionaryOutput.toByteArray());
    }

    public InvertedListSearcher getInvertedListSearcher() throws IOException {
        return new RAMInvertedListSearcher(invertedList);
    }

    @Override
    public NEXISearcher getNEXISearcher() throws IOException {
        return OwnSearcherFactory.getNEXISearcher(this, this);
    }

    public Dictionary getDictionary() {
        return dictionary;
    }
    
    public ReadOnlyDictionary getReadOnlyDictionary() {
        return dictionary;
    }

    public void close() {
    }

    @Override
    public WritableIndex getWritableIndex() throws IOException {
        return new OwnWritableIndex(this);
    }
    
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/